package XZK010404.dao;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 数据库连接与关闭 DAO
 *
 * @author bing.zhang06@hand-china.com 2021/7/7 21:29
 **/
public class BaseDao {
    private static Connection connection;

    static {
        try (FileInputStream fis = new FileInputStream("D:\\tmp\\kkbJobProject\\src\\db.properties")) {
            Properties properties = new Properties();
            properties.load(fis);
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = properties.getProperty("url");
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            connection = DriverManager.getConnection(url, user, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection() {
        return connection;
    }

    public static void close() {
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
